package com.woniuxy.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.woniuxy.mapper.HouseMapper;
import com.woniuxy.mapper.StudentMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.pojo.PageInfo;
import com.woniuxy.pojo.Student;
import com.woniuxy.pojo.User;
import com.woniuxy.service.StudentService;
import lombok.Data;

@Data
@Service
public class StudentServiceImp implements StudentService {
	@Resource
	private StudentMapper studentMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private HouseMapper houseMapper;

	// 分页显示学生信息
	@Override
	public PageInfo ShowAllByPage(int pageNumber) {
		PageInfo pageInfo = new PageInfo();
		// 每页显示的数量
		int pageSize = 5;
		// 学生总数
		int count = studentMapper.studentCount();
		// 计算总页数
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		if (pageNumber > pageCount) {
			// 下一页不能大于总页数
			return null;
		}
		// 取得起始索引
		pageNumber = (pageNumber - 1) * pageSize;
		pageInfo.setPageNumber(pageNumber);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(pageCount);
		List<Student> list = studentMapper.ShowAllByPage(pageInfo.getPageNumber(), pageInfo.getPageSize());
		pageInfo.setList(list);
		return pageInfo;
	}

	// 多条件查学生信息
	@Override
	public PageInfo select(Student student, int pageNumber) {
		PageInfo pageInfo = new PageInfo();
		// 每页显示的数量
		int pageSize = 5;
		// 学生总数
		int count = studentMapper.selectCount(student);
		System.out.println("符合条件学生总行数：" + count);
		// 计算总页数
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		if (pageNumber > pageCount) {
			// 下一页不能大于总页数
			return null;
		}
		// 取得起始索引
		pageNumber = (pageNumber - 1) * pageSize;

		pageInfo.setPageNumber(pageNumber);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(pageCount);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("student", student);
		map.put("pageNumber", pageNumber);
		map.put("pageSize", pageSize);
		List<Student> list = studentMapper.select(map);
		pageInfo.setList(list);

		return pageInfo;
	}

	@Override
	public Student findStudentById(int student_id) {
		return studentMapper.findStudentById(student_id);
	}

	// 添加学生--并添加角色信息
	@Override
	@Transactional
    public int add(Student student, int user_number) {

				student.setUser_number(user_number);
				// 添加学生信息
				int add = studentMapper.add(student);
				if (add != 0) {
					// 添加成功， 查询student权限id
					int findRoles = userMapper.findRoles("student");
					// 添加角色——权限信息
					int j = userMapper.addUserRole(user_number, findRoles);
					if (j == 0) {
						// 添加不成功
						return 0;
					}
				}else {
					return 0;
				}
				
                // 房间已住人数+1
                int update = houseMapper.inHouse(student.getHouse_id());
				return update;
			}


    // 通过学生信息添加到用户表.返回用户表主键id
    @Override
    @Transactional
    public int addUserByStudent(Student student) {
        // 判断用户是否已存在
        int number = userMapper.findUser_numberByPhone(student.getStudent_phone(),
                student.getStudent_name());
        System.out.println("检测用户是否存在返回值：" + number);
        if (number != 0) {
            System.out.println("用户已存在");
            return 0;
        }
        // 没有该用户就新建
        User user = new User(0, student.getStudent_name(), student.getStudent_phone(), student.getStudent_phone(),
                null);
        int i = userMapper.add(user);
        if (i != 0) {
            // 添加成功,返回id
            int user_number = userMapper.findUser_number();
            System.out.println("返回的user_number：" + user_number);
            return user_number;
        }
        return 0;
    }

    // 换房操作
    @Override
    @Transactional
    public int updateStudentHouse(int oldHouse_id, int newHouse_id, int student_id) {
        // oldHouse_id 旧房间id
        // house_id 新房间id

            // 1 原房间已住人数-1
        int i = houseMapper.outHouse(oldHouse_id);
            if (i != 0) {
                // 2 更新学生入住新房间id,
            int j = studentMapper.updateStudentHouse(newHouse_id, student_id);
            if (j != 0) {
                // 3 新房间已入住人数+1
                int k = houseMapper.inHouse(newHouse_id);
                if (k != 0) {
                    return k;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }

        }
        return i;

    }

}
